package com.platform.web.controller.data;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.base.domain.BaseLocation;
import com.platform.base.domain.ValveInfo;
import com.platform.base.service.IBaseLocationService;
import com.platform.base.service.IValveInfoService;
import com.platform.common.annotation.Log;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.BusinessType;
import com.platform.common.utils.poi.ExcelUtil;
import com.platform.data.domain.ValveRead;
import com.platform.data.service.IValveReadService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 阀门采集数据Controller
 * 
 * @author wangqi
 * @date 2020-09-27
 */
@Controller
@RequestMapping("/data/valve/valveread")
public class ValveReadController extends BaseController
{
    private String prefix = "data/valve/valveread";

    @Autowired
    private IValveReadService valveReadService;

    @Autowired
    private IValveInfoService valveInfoService;

    @Autowired
    private IBaseLocationService baseLocationService;

    @RequiresPermissions("data:valve:valveread:view")
    @GetMapping(value = { "", "/{valveId}" })
    public String data(@PathVariable(value = "valveId", required = false) String valveId, ModelMap mmap)
    {
        if(valveId==null){
            valveId = "";
        }
        mmap.put("valveId", valveId);
        mmap.put("location", new BaseLocation());
        return prefix + "/data";
    }

    /**
     * 根据地址查询阀门信息
     * @param locationId
     * @param mmap
     * @return
     */
    @RequiresPermissions("data:valve:valveread:view")
    @GetMapping("/location/{locationId}")
    public String location(@PathVariable(value = "locationId") String locationId, ModelMap mmap)
    {
        String valveIds = "";
        List<ValveInfo> list = valveInfoService.list(new QueryWrapper<ValveInfo>().eq("location_id", locationId));
        for(ValveInfo valveInfo : list){
            if(valveIds.equals("")){
                valveIds = valveInfo.getValveId();
            }else{
                valveIds = valveIds + "," + valveInfo.getValveId();
            }
        }
        mmap.put("valveId", valveIds);
        mmap.put("location", baseLocationService.getById(locationId));
        return prefix + "/data";
    }

    /**
     * 查询阀门采集数据列表
     */
    @RequiresPermissions("data:valve:valveread:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ValveRead valveRead)
    {
        startPage();
        if(valveRead.getValveId() !=null && !valveRead.getValveId().equals("")){
            valveRead.setLocationId(null);
        }
        List<ValveRead> list = valveReadService.selectValveReadList(valveRead);
        return getDataTable(list);
    }

    /**
     * 导出阀门采集数据列表
     */
    @RequiresPermissions("data:valve:valveread:export")
    @Log(title = "阀门采集数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ValveRead valveRead)
    {
        if(valveRead.getValveId() !=null && !valveRead.getValveId().equals("")){
            valveRead.setLocationId(null);
        }
        List<ValveRead> list = valveReadService.selectValveReadList(valveRead);
        ExcelUtil<ValveRead> util = new ExcelUtil<ValveRead>(ValveRead.class);
        return util.exportExcel(list, "阀门采集数据");
    }

    /**
     * 读取阀门数据
     */
    @Log(title = "读取阀门数据", businessType = BusinessType.OTHER)
    @RequiresPermissions("data:valve:valveread:check")
    @PostMapping("/check")
    @ResponseBody
    public AjaxResult check(String locationId) throws InterruptedException {

        String message = valveReadService.checkData(locationId);
        return AjaxResult.success(message);
    }
}
